clear all

global replica_dir="<add path to your working directory>"

global data_dir=	"$replica_dir/data"
global output_dir=	"$replica_dir/output"


global simce_data_dir="<add path to your SIMCE data directory>"
*------------------------------------------------------------------------------*

* SIMCE 2013 6b

*------------------------------------------------------------------------------*

**# Student scores

*------------------------------------------------------------------------------*

use "$simce_data_dir/6B/simce6b2013/Archivos DTA (Stata)/simce6b2013_alu_publica_final.dta", clear

keep if noptje_lect6b_alu==0 | noptje_mate6b_alu==0    

duplicates report mrun
duplicates examples mrun
duplicates tag mrun, g(ndupobs)
unique mrun

keep if ndupobs==0

rename  (ptje_lect6b_alu 	ptje_mate6b_alu) /// 
		(lect_post			mate_post)


keep idalumno mrun rbd lect_post mate_post 

save "$data_dir/simce_b6_2013.dta", replace

*------------------------------------------------------------------------------*

**#  School survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/6B/simce6b2013/Archivos DTA (Stata)/simce6b2013_rbd_publica_final.dta", clear

egen sch_stno_post = rowmax(nalu_6b_rbd nalu_lect6b_rbd nalu_mate6b_rbd)

rename 	(cod_depe1		cod_grupo 		cod_reg_rbd) ///
		(sch_depe1_post	sch_gse_post 	cod_reg_rbd_post)

keep  rbd sch_stno_post sch_depe1_post sch_gse_post cod_reg_rbd_post

merge 1:m rbd using "$data_dir/simce_b6_2013.dta"

////browse if _merge==1
keep if _merge==3
drop _merge


drop idalumno
rename rbd rbd_post
sort mrun

save "$data_dir/simce_b6_2013.dta", replace

*------------------------------------------------------------------------------*
